﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmSalesStatistics : BaseForm
    {
        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();
        private IBLL.ISacf sacfBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSacf();
        private IBLL.IGoods goodsBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteGoods();
        private IBLL.IType typeTjBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteType();
        private IBLL.ICustomer cusBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteCustomer();
        private IBLL.ISupply supplyBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteSupply();

        public FrmSalesStatistics()
        {
            InitializeComponent();
        }

        private void txtDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
                {
                    MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
                if (depart == null)
                {
                    MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtDepCode.Focus();
                    return;
                }
                this.txtBoxDep.Text = depart.DepName;
                this.txtGysCode.Focus();
            }
        }

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
                {
                    if (this.txtID.Text.Trim().Length < 4 || this.txtID.Text.Trim().Length > 13)
                    {
                        MessageBox.Show(string.Format("当前商品条码只有{0}位，请重新输入4到13位的商品条码！", this.txtID.Text.Trim().Length), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    MisEntity.Goods goods = goodsBLL.EndsWithID(this.txtID.Text.Trim());
                    if (goods == null)
                    {
                        MessageBox.Show("无此商品,请先设置！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtID.Focus();
                        return;
                    }
                    this.txtGoods.Text = goods.GOODS;
                    this.txtDM.Text = goods.DM;
                    this.txtID.Text = goods.ID;
                }
                this.txtDepCode.Focus();
            }
        }

        private void txtCusCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtCusCode.Text.Trim()))
                {
                    MisModel.Customer customer = cusBLL.GetById(this.txtCusCode.Text.Trim());
                    if (customer == null)
                    {
                        MessageBox.Show("无此代码或不属于本部分！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtCusCode.Focus();
                        return;
                    }
                    this.txtBoxCus.Text = customer.Customer_;
                }
                this.txtCusDepCode.Focus();
            }
        }

        private void txtType1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtType1.Text.Trim()))
                {
                    MisModel.Type type = typeTjBLL.GetType1ById(this.txtType1.Text.Trim());
                    if (type == null)
                    {
                        MessageBox.Show("统计类型不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtType1.Focus();
                        return;
                    }
                    else
                        this.textBox29.Text = type.TypeName;
                }
                this.txtType2.Focus();
            }
        }

        private void txtType2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtType2.Text.Trim()))
                {
                    MisModel.Type type = typeTjBLL.GetType1ById(this.txtType2.Text.Trim());
                    if (type == null)
                    {
                        MessageBox.Show("统计类型不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtType2.Focus();
                        return;
                    }
                    else
                        this.textBox31.Text = type.TypeName;
                }
                this.txtType3.Focus();
            }
        }

        private void txtType3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtType3.Text.Trim()))
                {
                    MisModel.Type type = typeTjBLL.GetType1ById(this.txtType3.Text.Trim());
                    if (type == null)
                    {
                        MessageBox.Show("统计类型不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtType3.Focus();
                        return;
                    }
                    else
                        this.textBox9.Text = type.TypeName;
                }
                this.btnOk.Focus();
            }
        }

        private void txtGysCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!string.IsNullOrEmpty(this.txtGysCode.Text.Trim()))
                {
                    MisModel.Supply supply = supplyBLL.GetById(this.txtGysCode.Text.Trim());
                    if (supply == null)
                    {
                        MessageBox.Show("供应商不存在,或无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtGysCode.Focus();
                        return;
                    }
                    else
                        this.txtGysName.Text = supply.SuppName;
                }
                this.txtCusCode.Focus();
            }  
        }

        private void openStarDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.openEndDate.Focus();
            }
        }

        private void openEndDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.expireStarDate.Focus();
            }
            
        }

        private void expireStarDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.expireEndDate.Focus();
            }
            
        }

        private void expireEndDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtNo.Focus();
            }
            
        }

        private void txtNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtShopCode.Focus();
            }
            
        }

        private void txtShopCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtID.Focus();
            }
            
        }

        private void txtCusDepCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtInvoiceNum.Focus();
            }
            
        }

        private void txtInvoiceNum_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtOper.Focus();
            }
            
        }

        private void txtOper_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtTax.Focus();
            }
            
        }

        private void txtTax_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtCusShopCode.Focus();
            }
            
        }

        private void txtCusShopCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.comboBox1.Focus();
            }
            
        }

        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtFKID.Focus();
            }
            
        }

        private void txtFKID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.comboBox2.Focus();
            }
            
        }

        private void comboBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.comboBox3.Focus();
            }
            
        }

        private void comboBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txtType1.Focus();
            }
            
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void btnRef_Click(object sender, EventArgs e)
        {
            Point mousePoint = MousePosition;
            Point point = this.PointToClient(mousePoint);
            this.contextMenuStrip1.Show(this, point);
        }

        private void FrmSalesStatistics_Load(object sender, EventArgs e)
        {
            this.expireStarDate.Format = DateTimePickerFormat.Custom;
            this.expireStarDate.CustomFormat = " ";
            this.expireEndDate.Format = DateTimePickerFormat.Custom;
            this.expireEndDate.CustomFormat = " ";
            this.txtDepCode.Text = accountUser.DeptCode;

            this.openStarDate.Value = DateTime.Now.Date.AddDays(-1);
            this.openStarDate.Focus();

            //业务员1初始化
            this.linker1.ValueMember = "CUS_ID";
            this.linker1.DisplayMember = "F2";
            this.linker1.DataSource = cusBLL.FindBusinessMan1();
            this.linker1.Text = "";
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtDepCode.Text.Trim()))
            {
                MessageBox.Show("部门代码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            MisModel.Depart depart = departBLL.GetById(this.txtDepCode.Text.Trim());
            if (depart == null)
            {
                MessageBox.Show("此部门无权操作！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtDepCode.Focus();
                return;
            }
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            StringBuilder sb = new StringBuilder();
            this.dataGridView1.Rows.Clear();
            if (!this.txtDepCode.Text.Trim().Equals("1"))
            {
                sb.Append(" and m.dep_id=@depId");
                dictionary.Add("@depId", this.txtDepCode.Text.Trim());
            }

            sb.Append(" and m.date>=@startDate and m.date<=@endDate");
            dictionary.Add("@startDate", this.openStarDate.Value.ToShortDateString());
            DateTime time = DateTime.Parse(this.openEndDate.Value.ToShortDateString());
            time = time.Date.AddDays(1);
            dictionary.Add("@endDate", time.ToShortDateString());
            if (!string.IsNullOrEmpty(this.expireStarDate.Text.ToString().Trim()))
            {
                sb.Append(" and m.d_kp>=@gqStartDate");
                dictionary.Add("@gqStartDate", this.expireStarDate.Value.ToShortDateString());
            }
            if (!string.IsNullOrEmpty(this.expireEndDate.Text.ToString().Trim()))
            {
                sb.Append(" and m.d_kp<=@gqEndDate");
                time = DateTime.Parse(this.expireEndDate.Value.ToShortDateString());
                time = time.Date.AddDays(1);
                dictionary.Add("@gqEndDate", time.ToShortDateString());
            }
            if (!string.IsNullOrEmpty(this.txtID.Text.Trim()))
            {
                sb.Append(" and m.id=@id");
                dictionary.Add("@id", this.txtID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtShopCode.Text.Trim()))
            {
                sb.Append(" and m.shop_id=@shopId");
                dictionary.Add("@shopId", this.txtShopCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtNo.Text.Trim()))
            {
                sb.Append(" and m.no=@no");
                dictionary.Add("@no", this.txtNo.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusCode.Text.Trim()))
            {
                sb.Append(" and m.cus_id=@cusId");
                dictionary.Add("@cusId", this.txtCusCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtGysCode.Text.Trim()))
            {
                sb.Append(" and g.supp_id=@suppId");
                dictionary.Add("@suppId", this.txtGysCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusDepCode.Text.Trim()))
            {
                sb.Append(" and m.cus_dep=@cusDep");
                dictionary.Add("@cusDep", this.txtCusDepCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtOper.Text.Trim()))
            {
                sb.Append(" and m.oper=@oper");
                dictionary.Add("@oper", this.txtOper.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtTax.Text.Trim()))
            {
                sb.Append(" and m.tax=@tax");
                dictionary.Add("@tax", this.txtTax.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtCusShopCode.Text.Trim()))
            {
                sb.Append(" and m.cus_shop=@cusShop");
                dictionary.Add("@cusShop", this.txtCusShopCode.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtBei.Text.Trim()))
            {
                sb.Append(" and m.bei=@bei");
                dictionary.Add("@bei", this.txtBei.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtInvoiceNum.Text.Trim()))
            {
                sb.Append(" and m.no_cus=@noCus");
                dictionary.Add("@noCus", this.txtInvoiceNum.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtFKID.Text.Trim()))
            {
                sb.Append(" and m.type_js=@typeJs");
                dictionary.Add("@typeJs", this.txtFKID.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtType1.Text.Trim()))
            {
                sb.Append(" and m.type1=@type1");
                dictionary.Add("@type1", this.txtType1.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtType2.Text.Trim()))
            {
                sb.Append(" and m.type2=@type2");
                dictionary.Add("@type2", this.txtType2.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtType3.Text.Trim()))
            {
                sb.Append(" and m.type3=@type3");
                dictionary.Add("@type3", this.txtType3.Text.Trim());
            }
            string tjStr = this.comboBox1.SelectedItem.ToString();
            if ("已开单".Equals(tjStr))
                sb.Append(" and m.f1='1'");
            else if ("已开单未确认提货".Equals(tjStr))
                sb.Append(" and m.f1='1' and m.f2=''");
            else if ("已确认未提货".Equals(tjStr))
                sb.Append(" and m.f2='0'");
            else if ("未提货,已开发票".Equals(tjStr))
                sb.Append(" and m.f2!='1' and m.f3='1'");
            else if ("已确认提货".Equals(tjStr))
                sb.Append(" and m.f2='1'");
            else if ("已提货,已开发票".Equals(tjStr))
                sb.Append(" and m.f2='1' and m.f3='1'");
            else if ("已提货,未开发票".Equals(tjStr))
                sb.Append(" and m.f2='1' and m.f3!='1'");
            else if ("已开发票".Equals(tjStr))
                sb.Append(" and m.f3='1'");
            else if ("已开发票,未到款".Equals(tjStr))
                sb.Append(" and m.f3='1' and m.f4!='1'");
            else if ("已开发票,已到款".Equals(tjStr))
                sb.Append(" and m.f3='1' and m.f4='1'");
            else if ("已结束".Equals(tjStr))
                sb.Append(" and m.f8='1'");
            tjStr = this.comboBox2.SelectedItem.ToString();
            if ("普通特价".Equals(tjStr))
                sb.Append(" and m.f6='1'");
            else if ("发票高开".Equals(tjStr))
                sb.Append(" and m.f6='2'");
            else if ("非特价".Equals(tjStr))
                sb.Append(" and m.f6=''");
            string tjType = this.comboBox3.SelectedItem.ToString();
            string linker1 = this.linker1.Text.Trim();
            DataTable dt = sacfBLL.GetStatisticsAll(sb.ToString(), dictionary, tjType, linker1); 
            decimal slp = 0;
            decimal sl = 0;
            decimal price1 = 0;
            decimal price2 = 0;
            decimal price3 = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                this.dataGridView1.Columns[1].Visible = false;
                this.dataGridView1.Columns[2].Visible = false;
                this.dataGridView1.Columns[3].Visible = false;
                this.dataGridView1.Columns[4].Visible = false;
                this.dataGridView1.Columns[5].Visible = false;
                if ("按单号客户分店业务员".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "客户";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "客户分店";
                    this.dataGridView1.Columns[3].HeaderCell.Value = "单据号";
                    this.dataGridView1.Columns[4].HeaderCell.Value = "开单日期";
                    this.dataGridView1.Columns[5].HeaderCell.Value = "业务";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                    this.dataGridView1.Columns[3].Visible = true;
                    this.dataGridView1.Columns[4].Visible = true;
                    this.dataGridView1.Columns[5].Visible = true;
                }
                else if ("按发票号客户部门".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "客户";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "客户部门";
                    this.dataGridView1.Columns[3].HeaderCell.Value = "发票号";
                    this.dataGridView1.Columns[4].HeaderCell.Value = "开票日期";
                    this.dataGridView1.Columns[5].HeaderCell.Value = "业务";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                    this.dataGridView1.Columns[3].Visible = true;
                    this.dataGridView1.Columns[4].Visible = true;
                    this.dataGridView1.Columns[5].Visible = true;
                }
                else if ("按发票号".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "发票号";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "客户";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                }
                else if ("按部门+商品".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "部门";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "商品";
                    this.dataGridView1.Columns[3].HeaderCell.Value = "";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                    this.dataGridView1.Columns[3].Visible = true;
                }
                else if ("按部门+客户".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "部门";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "客户";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                }
                else if ("按供应商+客户".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "供应商";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "客户";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                }
                else if ("按日期".Equals(tjType) || "按日期+商场".Equals(tjType) || "按日期+部门".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "日期";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "部门";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                }
                else if ("按仓库".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "部门";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "仓库";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                }
                else if ("按销售员".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "部门";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "销售";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                }
                else if ("按部门".Equals(tjType) || "按商场".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "部门";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                }
                else if ("按商品".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "商品";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                }
                else if ("按客户分店".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "客户编号";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "客户分店";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                }
                else if ("按客户部门".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "客户编号";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "客户部门";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                }
                else if ("按客户".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "客户编号";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                }
                else if ("按供应商".Equals(tjType))
                {
                    this.dataGridView1.Columns[1].HeaderCell.Value = "供应商";
                    this.dataGridView1.Columns[2].HeaderCell.Value = "";
                    this.dataGridView1.Columns[1].Visible = true;
                    this.dataGridView1.Columns[2].Visible = true;
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    String[] DataRow = new String[12];
                    DataRow[0] = (i + 1).ToString();
                    if ("按单号客户分店业务员".Equals(tjType) || "按发票号客户部门".Equals(tjType))
                    {
                        DataRow[1] = string.Format("{0} {1}", dt.Rows[i][2].ToString(), dt.Rows[i][5].ToString());
                        DataRow[2] = string.Format("{0} {1}", dt.Rows[i][3].ToString(), dt.Rows[i][6].ToString());
                        DataRow[3] = dt.Rows[i][0].ToString();
                        DataRow[4] = dt.Rows[i][1].ToString();
                        DataRow[5] = "";
                        DataRow[6] = dt.Rows[i][7].ToString();
                        DataRow[7] = dt.Rows[i][8].ToString();
                        DataRow[8] = dt.Rows[i][9].ToString();
                        DataRow[9] = dt.Rows[i][10].ToString();
                        DataRow[10] = string.Format("{0}", decimal.Parse(DataRow[7]) - decimal.Parse(DataRow[9]));
                        DataRow[11] = dt.Rows[i][11].ToString();
                    }
                    else if ("按发票号客户部门".Equals(tjType))
                    {
                        DataRow[1] = string.Format("{0} {1}", dt.Rows[i][2].ToString(), dt.Rows[i][5].ToString());
                        DataRow[2] = string.Format("{0} {1}", dt.Rows[i][3].ToString(), dt.Rows[i][6].ToString());
                        DataRow[3] = dt.Rows[i][0].ToString();
                        DataRow[4] = dt.Rows[i][1].ToString();
                        DataRow[5] = "";
                        DataRow[6] = dt.Rows[i][7].ToString();
                        DataRow[7] = dt.Rows[i][8].ToString();
                        DataRow[8] = dt.Rows[i][9].ToString();
                        DataRow[9] = dt.Rows[i][10].ToString();
                        DataRow[10] = string.Format("{0}", decimal.Parse(DataRow[7]) - decimal.Parse(DataRow[9]));
                        DataRow[11] = dt.Rows[i][11].ToString();
                    }
                    else if ("按部门+商品".Equals(tjType))
                    {
                        DataRow[1] = dt.Rows[i][0].ToString();
                        DataRow[2] = dt.Rows[i][1].ToString();
                        DataRow[3] = dt.Rows[i][2].ToString();
                        DataRow[6] = dt.Rows[i][3].ToString();
                        DataRow[7] = dt.Rows[i][4].ToString();
                        DataRow[8] = dt.Rows[i][5].ToString();
                        DataRow[9] = dt.Rows[i][6].ToString();
                        DataRow[10] = string.Format("{0}", decimal.Parse(DataRow[7]) - decimal.Parse(DataRow[9]));
                        DataRow[11] = dt.Rows[i][7].ToString();
                    }
                    else
                    {
                        DataRow[1] = dt.Rows[i][0].ToString();
                        DataRow[2] = dt.Rows[i][1].ToString();
                        DataRow[6] = dt.Rows[i][2].ToString();
                        DataRow[7] = dt.Rows[i][3].ToString();
                        DataRow[8] = dt.Rows[i][4].ToString();
                        DataRow[9] = dt.Rows[i][5].ToString();
                        DataRow[10] = string.Format("{0}", decimal.Parse(DataRow[7]) - decimal.Parse(DataRow[9]));
                        DataRow[11] = dt.Rows[i][6].ToString();
                    }
                    slp += decimal.Parse(DataRow[6]);
                    price1 += decimal.Parse(DataRow[7]);
                    price2 += decimal.Parse(DataRow[8]);
                    price3 += decimal.Parse(DataRow[9]);
                    sl += decimal.Parse(DataRow[11]);
                    this.dataGridView1.Rows.Add(DataRow);
                }
                this.label17.Text = string.Format("● 共{6}条 合计数量：{0} 含税额：{1} 未税额：{2} 平均成本：{3} 毛利：{4} 件数：{5}", (int)slp, price1, price2, price3.ToString("f4"), (price1 - price3).ToString("f4"), (int)sl, dt.Rows.Count);
            }
            else
            {
                this.label17.Text = "";
                MessageBox.Show("查无符合条件的记录！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void toolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void expireStarDate_ValueChanged(object sender, EventArgs e)
        {
            this.expireStarDate.Format = DateTimePickerFormat.Long;
            this.expireStarDate.CustomFormat = null;
        }

        private void expireEndDate_ValueChanged(object sender, EventArgs e)
        {
            this.expireEndDate.Format = DateTimePickerFormat.Long;
            this.expireEndDate.CustomFormat = null;
        }

    }
}
